package com.midi.wholesale.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ListModel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionListener;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.SwingConstants;
import java.awt.Component;
import javax.swing.JComboBox;
import java.awt.Dimension;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.ImageIcon;

import com.midi.wholesale.main.PasswordChecker;
import com.midi.wholesale.main.addElements;
import com.midi.wholesale.ui.DatePicker;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.midi.wholesale.main.*;
import com.midi.wholesale.models.Agent;
import com.midi.wholesale.models.Item;
import com.midi.wholesale.models.Lorry;
import com.midi.wholesale.models.Route;
import javax.swing.JList;

public class AddToLorry extends JFrame {

	private JPanel contentPane;
	
	public static final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
	private JTextField textDate;
	private JTable tableItems;
	private JTextField ci_itemName_textField;
	private JTextField ci_itemCode_textField;
	
	
	private JComboBox ld_route_combo = new JComboBox();
	private JComboBox ld_agent_combo = new JComboBox();
	
	private ArrayList lorryList;
	private JTextField de_date_text;
	private JTextField sd_amount_text;
	
	
	private ArrayList item;
	private int addElementTabClickCount;
	
	private JList item_list;
	DefaultListModel modelItem;
	static String ValItem;
	ListSelectionListener listSelectionListenerItem;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddToLorry frame = new AddToLorry();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddToLorry() {
		setResizable(false);
		final JFrame home = this;
		setTitle("WholeSale v1.0 - Lathpandura Holdings");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 538, 694);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		//contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		
		tabbedPane.setBounds(10,10,500,634);
		contentPane.add(tabbedPane);
		
		JPanel loadLorryPanel = new JPanel();
		loadLorryPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
		tabbedPane.addTab("Delivery", null, loadLorryPanel, null);
		loadLorryPanel.setLayout(null);
		
		JLabel lblLorry = new JLabel("Lorry");
		lblLorry.setBounds(57, 24, 46, 18);
		loadLorryPanel.add(lblLorry);
		
		JLabel lblAgent = new JLabel("Sales Agent");		
		lblAgent.setBounds(57, 65, 77, 14);
		loadLorryPanel.add(lblAgent);			
		
		
		final JComboBox ld_lorry_combo = new JComboBox();
		ld_lorry_combo.setBounds(198, 24, 168, 20);
		loadLorryPanel.add(ld_lorry_combo);
		
				
		final JComboBox ld_agent_combo = new JComboBox();
		ld_agent_combo.setBounds(198, 63, 168, 20);
		loadLorryPanel.add(ld_agent_combo);
		
				
		JLabel lblRoute = new JLabel("Route");
		lblRoute.setBounds(57, 107, 46, 14);
		loadLorryPanel.add(lblRoute);
		
		final JComboBox ld_route_combo = new JComboBox();
		ld_route_combo.setBounds(198, 105, 168, 20);
		loadLorryPanel.add(ld_route_combo);
		
		ld_lorry_combo.removeAllItems();
		ld_lorry_combo.insertItemAt("Choose Lorry", 0);
		ld_lorry_combo.setSelectedIndex(0);
		
		ld_agent_combo.removeAllItems();
		ld_agent_combo.insertItemAt("Choose Agent",0);
		ld_agent_combo.setSelectedIndex(0);					
	
		ld_route_combo.removeAllItems();
		ld_route_combo.insertItemAt("Choose Route",0);
		ld_route_combo.setSelectedIndex(0);
		
		ArrayList<Agent> agentList = Common.loadagents();
		for (Agent agent : agentList) {
			ld_agent_combo.addItem(agent);
		}
		
				
		ArrayList<Lorry> lorryList = Common.loadlorries();
		for (Lorry lorry : lorryList) {
			ld_lorry_combo.addItem(lorry);
		}
		
		ArrayList<Route> routeList = Common.loadroutes();
		for (Route route : routeList) {
			ld_route_combo.addItem(route);
		}
		JLabel lblDate = new JLabel("Date");
		lblDate.setBounds(57, 158, 46, 14);
		loadLorryPanel.add(lblDate);
		
		textDate = new JTextField();
		textDate.setBounds(198, 155, 168, 20);
		loadLorryPanel.add(textDate);
		textDate.setColumns(10);
		
		JButton buttonCalendar = new JButton("");
		buttonCalendar.setIcon(new ImageIcon(AddToLorry.class.getResource("/com/midi/wholesale/resources/date.gif")));
		buttonCalendar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				textDate.setText(new DatePicker(home).setPickedDate());
			}
		});
		buttonCalendar.setBounds(376, 154, 21, 23);
		loadLorryPanel.add(buttonCalendar);
		
		JScrollPane lorry_table_scrollPane = new JScrollPane();
		lorry_table_scrollPane.setBounds(57, 263, 340, 131);
		loadLorryPanel.add(lorry_table_scrollPane);
		
		tableItems = new JTable();
		lorry_table_scrollPane.setViewportView(tableItems);
		tableItems.setCellSelectionEnabled(true);
		tableItems.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null},
				{null, null},
				{null, null},
			},
			new String[] {
				"Item Name", "Amount"
			}
		));
		
		JButton lbl_btnLoadExisting = new JButton("Load Existing");
		lbl_btnLoadExisting.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				if ( !ld_lorry_combo.getSelectedItem().toString().equals("Choose Lorry") && 
						!ld_agent_combo.getSelectedItem().toString().equals("Choose Agent") &&
						!ld_route_combo.getSelectedItem().toString().equals("Choose Route") ) {
					ArrayList<HashMap<String,String>> items = Common.loadDelivery((Lorry)ld_lorry_combo.getSelectedItem(),
							(Agent)ld_agent_combo.getSelectedItem(), (Route)ld_route_combo.getSelectedItem());
					Object[][] modelArray = new Object[items.size()][2];
					for ( int i = 0; i < items.size(); i++ ) {
						modelArray[i][0] = items.get(i).get("item_name");
						modelArray[i][1] = items.get(i).get("amount");
					}
					tableItems.setModel(new DefaultTableModel(modelArray,
							new String[] {
								"Item Name", "Amount"
							}
						));
				}
			}
		});
		lbl_btnLoadExisting.setBounds(57, 443, 132, 23);
		loadLorryPanel.add(lbl_btnLoadExisting);
		
		JButton lbl_btnSubmitChanges = new JButton("Submit Changes");
		lbl_btnSubmitChanges.setBounds(246, 443, 151, 23);
		loadLorryPanel.add(lbl_btnSubmitChanges);
		
		//////////////////////////////////////////////////////////////////////
		//create item panel
		JPanel addElementPanel = new JPanel();
		tabbedPane.addTab("Add Elements", null, addElementPanel, null);
		addElementPanel.setLayout(null);
		
		JList item_list = new JList();
		item_list.setBounds(251, 38, 223, 98);
		addElementPanel.add(item_list);
		
		JPanel itemPanel = new JPanel();
		itemPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		itemPanel.setLayout(null);
		itemPanel.setBounds(8, 11, 483, 176);
		addElementPanel.add(itemPanel);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(235, 11, 238, 127);
		itemPanel.add(scrollPane);
		
		ListModel modelAgency;
		item_list = new JList();
		scrollPane.setViewportView(item_list);
		item_list.setFont(new Font("Arial", Font.BOLD, 11));
		item_list.setBorder(new TitledBorder(null, "Item List", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		item_list.setBackground(UIManager.getColor("Button.highlight"));
		
		JLabel lblItemName = new JLabel("Item Name");
		lblItemName.setBounds(20, 64, 82, 14);
		itemPanel.add(lblItemName);
		
		ci_itemName_textField = new JTextField();
		ci_itemName_textField.setBounds(91, 64, 92, 14);
		itemPanel.add(ci_itemName_textField);
		ci_itemName_textField.setColumns(10);
		
		JLabel lblItemCode = new JLabel("Item Code");
		lblItemCode.setBounds(20, 90, 82, 14);
		itemPanel.add(lblItemCode);
		
		ci_itemCode_textField = new JTextField();
		ci_itemCode_textField.setBounds(91, 90, 92, 14);
		itemPanel.add(ci_itemCode_textField);
		ci_itemCode_textField.setColumns(10);
		
		
		JButton ci_save_btn = new JButton("Save");
		ci_save_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String new_itemName = ci_itemName_textField.getText();
				String new_itemCode = ci_itemCode_textField.getText();

				if (new_itemName.equals("") ||new_itemCode.equals("") ) {
					JOptionPane.showMessageDialog(null,
							"Please enter all fields...");
				} else {
					try {						
												
						boolean IsRecorded=new CreateItem().submitData(new_itemName, new_itemCode);
						
						if(IsRecorded){
							JOptionPane.showMessageDialog(null, "Item created Successfully.");
							ci_itemName_textField.setText("");
							ci_itemCode_textField.setText("");
						
						}
						
						
					} catch (NumberFormatException nfe) {
						JOptionPane.showMessageDialog(null, "Invalid Format...");
					}
				}	
				
			}
		});
		ci_save_btn.setBounds(10, 115, 82, 23);
		itemPanel.add(ci_save_btn);
		
		JButton ci_clear_btn = new JButton("Clear");
		ci_clear_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ci_itemName_textField.setText("");
				ci_itemCode_textField.setText("");
			}
		});
		ci_clear_btn.setBounds(101, 115, 82, 23);
		itemPanel.add(ci_clear_btn);
		
		JLabel lblAddItem = new JLabel("Add Item");
		lblAddItem.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblAddItem.setBounds(22, 11, 95, 36);
		itemPanel.add(lblAddItem);
		
		JButton ad_ItemRemoveButton = new JButton("Remove");
		ad_ItemRemoveButton.setBounds(384, 142, 89, 23);
		itemPanel.add(ad_ItemRemoveButton);
		
	
		//////////////////////////////////////////////////////////////////////
		//Day End panel
		JPanel dayEndPanel = new JPanel();
		tabbedPane.addTab("Day End", null, dayEndPanel, null);
		dayEndPanel.setLayout(null);
		
		JLabel de_lorry_text = new JLabel("Lorry");
		de_lorry_text.setBounds(125, 44, 46, 14);
		dayEndPanel.add(de_lorry_text);
		
		final JComboBox de_lorry_comboBox = new JComboBox();
		de_lorry_comboBox.setBounds(225, 36, 129, 20);
		dayEndPanel.add(de_lorry_comboBox);
		
		JLabel de_agent_lbl = new JLabel("Sales Agent");
		de_agent_lbl.setBounds(125, 89, 90, 14);
		dayEndPanel.add(de_agent_lbl);
		
		final JComboBox de_agent_comboBox = new JComboBox();
		de_agent_comboBox.setBounds(225, 81, 129, 20);
		dayEndPanel.add(de_agent_comboBox);
		
		JLabel de_route_lbl = new JLabel("Route");
		de_route_lbl.setBounds(125, 140, 46, 14);
		dayEndPanel.add(de_route_lbl);
		
		final JComboBox de_route_comboBox = new JComboBox();
		de_route_comboBox.setBounds(225, 132, 129, 20);
		dayEndPanel.add(de_route_comboBox);
		
		JButton de_save_btn = new JButton("Save");
		de_save_btn.setBounds(126, 427, 89, 23);
		dayEndPanel.add(de_save_btn);
		
		JButton de_clear_btn = new JButton("Clear");
		de_clear_btn.setBounds(225, 427, 89, 23);
		dayEndPanel.add(de_clear_btn);
		
		JLabel de_date_lbl = new JLabel("Date");
		de_date_lbl.setBounds(125, 184, 46, 14);
		dayEndPanel.add(de_date_lbl);
		
		de_date_text = new JTextField();
		de_date_text.setBounds(225, 176, 129, 20);
		dayEndPanel.add(de_date_text);
		de_date_text.setColumns(10);
		
		
		JButton de_datePick_btn = new JButton("");
		de_datePick_btn.setIcon(new ImageIcon(AddToLorry.class.getResource("/com/midi/wholesale/resources/date.gif")));
		de_datePick_btn.setBounds(364, 175, 21, 23);
		dayEndPanel.add(de_datePick_btn);
		
		JButton de_view_btn = new JButton("View");
		de_view_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
			}
		});
		de_view_btn.setBounds(172, 207, 89, 23);
		dayEndPanel.add(de_view_btn);
		
		de_datePick_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				de_date_text.setText(new DatePicker(home).setPickedDate());
			}
		});
		
		
	
		//////////////////////////////////////////////////////////////////////
		//Stock Details panel
		JPanel stockDetailPanel = new JPanel();
		tabbedPane.addTab("Stock Detail", null, stockDetailPanel, null);
		stockDetailPanel.setLayout(null);
		
		JLabel sd_itemName_lbl = new JLabel("Item Name");
		sd_itemName_lbl.setBounds(130, 49, 73, 14);
		stockDetailPanel.add(sd_itemName_lbl);
		
		final JComboBox sd_itemName_comboBox = new JComboBox();
		sd_itemName_comboBox.setBounds(253, 46, 133, 20);
		stockDetailPanel.add(sd_itemName_comboBox);
		
		JLabel sd_amount_lbl = new JLabel("Amount");
		sd_amount_lbl.setBounds(130, 93, 61, 14);
		stockDetailPanel.add(sd_amount_lbl);
		
		sd_amount_text = new JTextField();
		sd_amount_text.setBounds(253, 90, 133, 20);
		stockDetailPanel.add(sd_amount_text);
		sd_amount_text.setColumns(10);
		
		JButton sd_save_btn = new JButton("Save");
		sd_save_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Object selected_item = sd_itemName_comboBox.getSelectedItem();
				String amount=sd_amount_text.getText();
				if(selected_item.equals("Choose Item")||selected_item==null||amount==null||amount.equals(""))
					{
						JOptionPane.showMessageDialog(null,"Please enter all fields...");
						
					}
				else
					{
					try {						
						
						Item new_item=(Item) selected_item;	
						int new_amount = Integer.parseInt(sd_amount_text.getText());
						int itemId=new_item.getId();
						boolean IsRecorded=new StockDetail().submitData(new_item.getId(),new_amount);
											
						/*
						
						if(IsRecorded){
							JOptionPane.showMessageDialog(null, "Bill Entry Added Successfully.");
							be_billNo.setText("");
							be_amnt_textField.setText("");
							be_des_textArea.setText("");
							be_agency_comboBox.setSelectedIndex(0);
							be_region_comboBox.setSelectedIndex(0);
							be_shop_comboBox.setSelectedIndex(0);*/
						//}
						
						
					} catch (NumberFormatException nfe) {
						JOptionPane.showMessageDialog(null, "Invalid Format...");
					}
						
					}		

		
			}
		});
		sd_save_btn.setBounds(156, 147, 89, 23);
		stockDetailPanel.add(sd_save_btn);
		
		JButton sd_clear_btn = new JButton("Clear");
		sd_clear_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				sd_itemName_comboBox.setSelectedIndex(0);
				sd_amount_text.setText("");
			}
		});
		sd_clear_btn.setBounds(258, 147, 89, 23);
		stockDetailPanel.add(sd_clear_btn);


		/*Check which tab is selected */
		tabbedPane.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				int tabId = tabbedPane.getSelectedIndex();
				System.out.println(tabId);

				switch (tabId) {

				case 0:	
					//Lorry details tab
					ld_lorry_combo.removeAllItems();
					ld_lorry_combo.insertItemAt("Choose Lorry", 0);
					ld_lorry_combo.setSelectedIndex(0);
					
					ld_agent_combo.removeAllItems();
					ld_agent_combo.insertItemAt("Choose Agent",0);
					ld_agent_combo.setSelectedIndex(0);					
				
					ld_route_combo.removeAllItems();
					ld_route_combo.insertItemAt("Choose Route",0);
					ld_route_combo.setSelectedIndex(0);
					
					
					ArrayList<Lorry> lorryList = Common.loadlorries();
					for (Lorry lorry : lorryList) {
						ld_lorry_combo.addItem(lorry);
					}
					
					ArrayList<Agent> agentList = Common.loadagents();
					for (Agent agent : agentList) {
						ld_agent_combo.addItem(agent);
					}
					
					ArrayList<Route> routeList = Common.loadroutes();
					for (Route route : routeList) {
						ld_route_combo.addItem(route);
					}
					break;

				case 1:
				//Add Element tab
					int id=2;
					boolean check=PasswordChecker.checkResult(id);
				   // Uses boolean to determine if password is valid
				   if(check==true)
				   {
					   System.out.println("Correct password");
					   
				   }
				   else
			       {
			       JOptionPane.showMessageDialog(null, "Enter Correct Password");
			       tabbedPane.setSelectedIndex(0);
			       }
					
					
					break;
				case 2:
					//Day End tab
					de_lorry_comboBox.removeAllItems();
					de_lorry_comboBox.insertItemAt("Choose lorry",0);
					de_lorry_comboBox.setSelectedIndex(0);
					
					de_agent_comboBox.removeAllItems();
					de_agent_comboBox.insertItemAt("Choose Agent",0);
					de_agent_comboBox.setSelectedIndex(0);					
				
					de_route_comboBox.removeAllItems();
					de_route_comboBox.insertItemAt("Choose Route",0);
					de_route_comboBox.setSelectedIndex(0);
					
					ArrayList<Lorry> de_lorryList = Common.loadlorries();
					for (Lorry lorry : de_lorryList) {
						de_lorry_comboBox.addItem(lorry);
					}
					
					ArrayList<Agent> de_agentList = Common.loadagents();
					for (Agent agent : de_agentList) {
						de_agent_comboBox.addItem(agent);
					}
					
					ArrayList<Route> de_routeList = Common.loadroutes();
					for (Route route : de_routeList) {
						de_route_comboBox.addItem(route);
					}
					
					break;
				case 3:
					//Stock Detail tab
					sd_itemName_comboBox.removeAllItems();
					sd_itemName_comboBox.insertItemAt("Choose Item",0);
					sd_itemName_comboBox.setSelectedIndex(0);
					
					ArrayList<Item> sd_itemList = Common.loadItems();
					for (Item item : sd_itemList) {
						sd_itemName_comboBox.addItem(item);
					}
					
					break;					
					
				}
			}
		
		});
	
				
	}
}
